// pages/activityApply/activityApply.js
const WxParse = require('../../../wxParse/wxParse.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsId: 0,
        detail: null,
      applyTop:10000,
      is_applyBtnShow:true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let type = options.type || '1';
        this.setData({
            type: type
        });

        this.data.goodsId = options.id;
        this.onPullDownRefresh();

      let that = this;
      setTimeout(function(){
        const query = wx.createSelectorQuery();
        query.select('#applyBox').boundingClientRect();
        query.selectViewport().scrollOffset();
        query.exec(function (res) {
          let height = res[0].height;  //获取内容区域的高度
          console.log(res)
          that.setData({
            applyTop: res[0].top
          })

        })
      },800)
      
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.loadDetail().finally(() => {
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        const image = "";
        return {
            imageUrl: image,
            path: `/pages/activity/detail/detail?id=${this.data.goodsId}&type=${this.data.type}`
        }
    },

    // 加载商品详情
    loadDetail: function() {
        return wx.http.get('/api/activity/detail', {
            goods_id: this.data.goodsId
        }).then((res) => {
            const detail = res.data.data.detail;
            this.setData({
                detail: detail,
            }, () => {
                WxParse.wxParse('detailContent', 'html', detail.activity_content, this);
            });
            return detail;
        }, (err) => {
            console.warn("/api/activity/detail", err)
        });
    },
  onPageScroll:function(e){
    let applyTop = this.data.applyTop;
    let is_applyBtnShow =  this.data.is_applyBtnShow;
    if (is_applyBtnShow && e.scrollTop > applyTop -500){
      this.setData({
        is_applyBtnShow: false,
      })
    }
    if (!is_applyBtnShow && e.scrollTop < applyTop - 500){
      this.setData({
        is_applyBtnShow: true,
      })
    }
  },
    // 点击报名
  applyBtnTap:function(){
    wx.pageScrollTo({
      scrollTop: this.data.applyTop,
      duration: 500
    })
  },

    // 提交
    onSubmit: function(e) {
        const values = e.detail.value;
        wx.http.post('api/activity/participate', {
            activity_id: this.data.goodsId,
            user_phone: values.phone,
            user_name: values.name
        }, {
            showLoading: true
        }).then((res) => {
            console.log(res);
            wx.showModal({
                content: res.data.msg,
                showCancel: false,
            })
        });
    },
});